package com.edu.chat.web.config;

import com.edu.buservice.util.model.TenantUtil;
import org.springframework.core.task.TaskDecorator;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-04-06 12:51
 **/


public class ContextCopyingDecorator implements TaskDecorator {
	@Override
	public Runnable decorate(Runnable runnable) {
		// 捕获父线程上下文
		String tenantId = TenantUtil.getTenantId();
		String userId = TenantUtil.getUserId();

		return () -> {
			try {
				// 将父线程上下文注入子线程
				TenantUtil.setCurrentTenantId(tenantId);
				TenantUtil.setCurrentUserId(userId);
				runnable.run();
			} finally {
				TenantUtil.clear();
			}
		};
	}
}
